-module(index).
-export([index/1]).

index(File) ->
    ets:new(indexTable, [ordered_set, named_table]),
    processFile(File),
    prettyIndex().

processFile(File) ->
    {ok, IoDevice} = file:open(File, [read]),
    processLines(IoDevice, 1).

processLines(IoDevice, N) ->
    case io:get_line(IoDevice, "") of
	eof ->	
	    ok;
	Line ->
	    processLine(Line, N),
	    processLines(IoDevice, N+1)
    end.	

-define(Punctuation, "(\\ |\\,|\\.|\\;|\\:|\\t|\\n|\\(|\\))+").

processLine(Line, N) ->
    case regexp:split(Line, ?Punctuation) of
	{ok, Words} ->
	    processWords(Words, N);
	_ -> []
    end.

processWords(Words, N) ->
    case Words of
	[] -> ok;
	[Word|Rest] ->
	    if 
		length(Word) > 3 ->
		    Normalise =  string:to_lower(Word),
		    ets:insert(indexTable, {{Normalise, N}});
		true -> ok
	    end,
	    processWords(Rest, N)
    end.

prettyIndex() ->
    Table = ets:first(indexTable), 
    Results = processTable(Table),
    io:format("~p~n", [Results]).

processTable(Record) ->
    case Record of
	'$end_of_table' -> ok;
	_-> 
	    {Word, _Linum} = Record,
	    NextRecord = readUntilNextWord(Record), 
	    [formElement(Word, Record, NextRecord) | 
	     processTable(NextRecord)]
    end.


readUntilNextWord(Record) ->
    NextRecord = ets:next(indexTable, Record),
    case NextRecord of
	'$end_of_table' -> NextRecord;
	%% 如果Record与NextRecord的Word相同，那么取下一个记录，否则返回
	_ ->	{Word1, _Linum1} = Record,
		{Word2, _Linum2} = NextRecord,
		case Word1 of 
		    Word2 -> readUntilNextWord(NextRecord);
		    _ -> NextRecord
		end
    end.


formElement(Word, Record, NextRecord) ->
    LinumList = formLinumList(Record, NextRecord),
						%LinumString = convertListToString(LinumList),
    {Word, LinumList}.

formLinumList(Record, NextRecord) -> 
    case Record of
	NextRecord -> []; 
	_ -> {_Word, Linum} = Record, 
	     NRecord = ets:next(indexTable, Record),
	     [Linum | formLinumList(NRecord, NextRecord)]
    end.

%% convertListToString的一点思路，把 [1 3 4 5 7 8 10 13 15]看作是区间的集合，
%% 即[1] [3 5] [7 8] [10] [13] [15]



